package com.zhiqingchun.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.common.Page;
import com.zhiqingchun.dao.account.IUserDao;
import com.zhiqingchun.dao.account.UserResult;
import com.zhiqingchun.dao.article.IArticleDao;
import com.zhiqingchun.pojo.ArticleDO;

@Controller
@RequestMapping("/userArticle.html")
public class UserArticleController {

	@RequestMapping(method = RequestMethod.GET)
	public String userArticle(ModelMap map, HttpServletRequest request, HttpServletResponse response, Page page) {
		String _id = request.getParameter("id");
		int id = 0;
		if (StringUtils.isNotBlank(_id)) {
			id = Integer.parseInt(_id);
		}
		ArticleDO articleDO = new ArticleDO();
		articleDO.setAuthor(id);
		List<ArticleDO> list = articleDao.list(articleDO, page);
		map.addAttribute("list", list);
		
		UserResult userResult = userDao.byId(id);
		if (userResult.isSuccess()) {
			map.addAttribute("userInfo", userResult.getUsr());
		}
		return "user/userArticle";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String userArticle2(ModelMap map, HttpServletRequest request, HttpServletResponse response, Page page) {
		return userArticle(map, request, response, page);
	}
	
	@Autowired
	private IArticleDao articleDao;
	
	@Autowired
	private IUserDao userDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	
}
